/**
 * Copyright phonemap.com
 *
 * Licensed under the phonemap.com License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.phonemap.com/licenses/LICENSE-1.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * Do login.
 */
 function doLogin() {
	document.location.href = '/login.html';
}

window.fbAsyncInit = function() {
	FB.init({
		appId      : '1435764669997973',//phonemaptest
//		appId : '157906274356332',//phonemap
		status : true, // check login status
		cookie : true, // enable cookies to allow the server to access the session
		xfbml : true,
		version : 'v2.0' // use version 2.0
	// parse XFBML
	});
	
	FB.Event.subscribe('auth.logout', function(response) {
	      alert('logged out');
	  }); 
	
	// check facebook login status
    FB.getLoginStatus(function(response) {
            console.log("FB login status: " + response.status);
            if (response.status === 'connected') {
                    //showWelcome();  //display welcome message
            	afterLogin();
            } else if (response.status === 'not_authorized') {
                    // the user is logged in to Facebook, but not connected to the app
                    //showFbLogin();  //display Facebook Login button
            	doLogin();
            } else {
                    // the user isn't even logged in to Facebook.
                    //showFbLogin();  //display Facebook Login button
            	doLogin();
            }
    });

	// Here we subscribe to the auth.authResponseChange JavaScript event. This event is fired
	// for any authentication related change, such as login, logout or session refresh. This means that
	// whenever someone who was previously logged out tries to log in again, the correct case below 
	// will be handled. 
	FB.Event.subscribe('auth.authResponseChange', function(response) {

	});
};

// Load the SDK asynchronously
(function(d) {
	var js, id = 'facebook-jssdk', ref = d
			.getElementsByTagName('script')[0];
	if (d.getElementById(id)) {
		return;
	}
	js = d.createElement('script');
	js.id = id;
	js.async = true;
	js.src = "//connect.facebook.net/de_DE/sdk.js";
	ref.parentNode.insertBefore(js, ref);
}(document));

 